package com.example.kuake.controller;

import com.alibaba.excel.EasyExcel;
import com.example.kuake.entity.QuarkFileExport;
import com.example.kuake.entity.QuarkFileListResponse;
import com.example.kuake.service.QuarkService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 夸克网盘控制器
 */
@RestController
@RequestMapping("/api/quark")
@Api(tags = "夸克网盘接口", description = "夸克网盘文件管理相关API")
public class QuarkController {

    @Autowired
    private QuarkService quarkService;

    @GetMapping("/files")
    @ApiOperation(value = "获取文件列表", notes = "获取指定文件夹下的文件列表")
    public QuarkFileListResponse getFileList(
            @ApiParam(value = "文件夹ID，默认为0表示根目录", example = "0")
            @RequestParam(defaultValue = "0") String folderId,
            
            @ApiParam(value = "页码，从1开始", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            
            @ApiParam(value = "每页大小", example = "20")
            @RequestParam(defaultValue = "20") Integer pageSize,

            @ApiParam(value = "夸克网盘Cookie", example = "b-user-id=5a1115de-b78b-58d9-4ab8-570a593dc90b; __sdid=AARThxIYs1ScLLZ7UtVjoFAOHtc4mAEGuKdbhg3GY/+IH1QiYKz0hJzcl2Kz0QFaIqZqRVqu62E3ONXLjl9k3MPS5ZmBfPKPqfiWuoSAPSEqbg==; _UP_D_=pc; _UP_A4A_11_=wb9cd148bcea499e9e76a0f5eb49ae3b; isg=BNTUYE-UQEXmVtR1wDzAmL1CpRJGLfgXZe7s1W61T9_iWXSjlj6RpTddXVFBoTBv; tfstk=gZUSHZwRuLv53YEtV252cvdDhX3CQs7NNBGLsWLy943JJJNn3b-PqDWQcJeqezSoU-MQ_WYEU7uROJwTFDyQt_cKpJeILuSV77Vo-2BadN7aZOwUDXaSJpPxrXrUKbKl77VkMQ9d_oQwOc0m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e232710885ca4fAATXmFcRpDVjFAgZHGxdpbk6wJAlK/8a9Q+KcsfGOU64+OYLHRinlueMgcsT8YSFP2LFAiq1K9D9gLFSBe24Do4ikoTy00JgUVal2Jg8kSFyINOzZneWqBqkkFJlwG5ynXs+zZElNHO4UZ4TQ5+rzePrF9zxTDAE11SrPTU1zqVdcpxYZQR7uwssQtVvJuBRX1lkxDj26RvjbE0IyOMspZDB")
            @RequestParam(defaultValue = "20") String cookie,

            @ApiParam(value = "识别码 eagleeyeTraceid", example = "0bf8fe6317578302780796642ee7a1")
            @RequestParam(defaultValue = "0bf8fe6317578302780796642ee7a1")String eagleeyeTraceid)
    {
        return quarkService.getFileList(folderId, page, pageSize, cookie,eagleeyeTraceid);
    }


    @PostMapping("/rename")
    @ApiOperation(value = "重命名文件", notes = "重命名指定文件")
    public String renameFile(
            @ApiParam(value = "文件ID", example = "09fdfb8d18914d4ebb62386d5c50e75b")
            @RequestParam String fid,
            
            @ApiParam(value = "新文件名", example = "新文件名.mp4")
            @RequestParam String fileName,
            
            @ApiParam(value = "夸克网盘Cookie", example = "b-user-id=xxx; __sdid=xxx")
            @RequestParam String cookie,
            
            @ApiParam(value = "识别码 eagleeyeTraceid", example = "0bf8fe6317578302780796642ee7a1")
            @RequestParam(defaultValue = "0bf8fe6317578302780796642ee7a1") String eagleeyeTraceid)
    {
        return quarkService.renameFile(fid, fileName, cookie, eagleeyeTraceid);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出所有文件名", notes = "导出所有文件名和文件夹名为Excel格式")
    public ResponseEntity<byte[]> exportAllFiles(
            @ApiParam(value = "夸克网盘Cookie", example = "b-user-id=xxx; __sdid=xxx")
            @RequestParam String cookie,
            
            @ApiParam(value = "识别码 eagleeyeTraceid", example = "0bf8fe6317578302780796642ee7a1")
            @RequestParam(defaultValue = "0bf8fe6317578302780796642ee7a1") String eagleeyeTraceid)
    {
        try {
            byte[] excelData = quarkService.exportAllFiles(cookie, eagleeyeTraceid);
            
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "quark_files.xlsx");
            headers.setContentLength(excelData.length);
            
            return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
            
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/exportOne")
    @ApiOperation(value = "导出指定目录的文件名", notes = "导出所有文件名和文件夹名为Excel格式")
    public void exportOneFiles(
            @ApiParam(value = "文件夹ID，默认为0表示根目录", example = "0")
            @RequestParam(defaultValue = "0") String folderId,

            @ApiParam(value = "夸克网盘Cookie", example = "b-user-id=xxx; __sdid=xxx")
            @RequestParam String cookie,

            @ApiParam(value = "识别码 eagleeyeTraceid", example = "0bf8fe6317578302780796642ee7a1")
            @RequestParam(defaultValue = "0bf8fe6317578302780796642ee7a1") String eagleeyeTraceid)
    {
        try {
            List<QuarkFileExport> excelData = quarkService.exportOneFiles( folderId,cookie, eagleeyeTraceid);

//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//            headers.setContentDispositionFormData("attachment", "quark_files.xlsx");
//            headers.setContentLength(excelData.length);
            EasyExcel.write("123123123", QuarkFileExport.class).sheet("模板").doWrite(excelData);

//            return new ResponseEntity<>(excelData, headers, HttpStatus.OK);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/batch-rename")
    @ApiOperation(value = "批量重命名", notes = "根据Excel文件批量重命名文件和文件夹")
    public String batchRename(
            @ApiParam(value = "重命名数据JSON", example = "[{\"fid\":\"xxx\",\"oldName\":\"old.txt\",\"newName\":\"new.txt\"}]")
            @RequestBody String renameData,
            
            @ApiParam(value = "夸克网盘Cookie", example = "b-user-id=xxx; __sdid=xxx")
            @RequestParam String cookie,
            
            @ApiParam(value = "识别码 eagleeyeTraceid", example = "0bf8fe6317578302780796642ee7a1")
            @RequestParam(defaultValue = "0bf8fe6317578302780796642ee7a1") String eagleeyeTraceid)
    {
        return quarkService.batchRename(renameData, cookie, eagleeyeTraceid);
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取网盘信息", notes = "获取夸克网盘基本信息")
    public String getQuarkInfo() {
        return "夸克网盘API服务运行正常！\n" +
               "支持功能：\n" +
               "1. 获取文件列表\n" +
               "2. 重命名文件\n" +
               "3. 导出所有文件名\n" +
               "4. 批量重命名\n" +
               "\n接口地址：\n" +
               "- GET /api/quark/files - 获取文件列表\n" +
               "- POST /api/quark/rename - 重命名文件\n" +
               "- GET /api/quark/export - 导出所有文件名\n" +
               "- POST /api/quark/batch-rename - 批量重命名\n" +
               "\n请求参数：\n" +
               "- folderId: 文件夹ID，默认为0表示根目录\n" +
               "- page: 页码，从1开始，默认1\n" +
               "- pageSize: 每页大小，默认20\n" +
               "- Cookie: 夸克网盘Cookie（必需）\n" +
               "- eagleeyeTraceid: 追踪ID（可选）\n" +
               "\n注意：\n" +
               "1. 使用真实夸克网盘API\n" +
               "2. 响应数据支持gzip解压缩\n" +
               "3. 支持JSON格式响应解析";
    }

}